package com.twu.biblioteca;

import com.twu.biblioteca.util.InputParser;
import com.twu.biblioteca.util.Messages;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: twer
 * Date: 5/14/13
 * Time: 9:17 PM
 * To change this template use File | Settings | File Templates.
 */
public class BibliotecaSystem {

    private Scanner scanner;

    public BibliotecaSystem(Scanner scanner) {
        this.scanner = scanner;
    }

    public void run() {
        this.showWelcomeMessage();

        int menuInput = 0;
        do {
            try {
                this.showMenuOptions();

                String select = scanner.nextLine();
                menuInput = InputParser.parseUserSelect(select);

                switch (menuInput) {
                    case 0:
                        break;
                    case 1:
                        showBooksList();
                        break;
                    case 2:
                        reserveBook();
                        break;
                    case 3:
                        checkMembershipDetails();
                        break;
                    default:
                        showNotifyMessage();
                        break;
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } while (menuInput != 0);
    }

    private void showWelcomeMessage() {
        System.out.println(Messages.Welcome_Message);
    }

    private void showMenuOptions() {
        System.out.println(Messages.Menu_Options);
    }

    private void showBooksList() {
        for (Book book : BibliotecaModel.getBooks()) {
            System.out.println(book.toString());
        }
    }

    private void reserveBook() {
        System.out.print("Select a book to reserve by id:");

        String input = scanner.nextLine();
        int selected = InputParser.parseUserSelect(input);
        if (selected <= 0 || selected > BibliotecaModel.getBooks().size()) {
            return;
        }

        Book book = BibliotecaModel.getBooks().get(selected - 1);
        if (book.isAvailable()) {
            System.out.println(Messages.Reserved_Successfully);
            book.setAvailable(false);
        } else {
            System.out.println(Messages.Reserved_UnSuccessfully);
        }
    }

    private void checkMembershipDetails() {
        System.out.println(Messages.Check_Membership_Details);
    }

    private void showNotifyMessage() {
        System.out.println(Messages.Notify_Message);
    }

    public void setScanner(Scanner scanner) {
        this.scanner = scanner;
    }
}
